package com.example.smallmall.component;


import com.example.smallmall.controller.FileController;
import com.example.smallmall.dto.CodeDto;
import com.example.smallmall.dto.LiveWeather;
import com.example.smallmall.dto.WeatherDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

@Component
public class WeatherManager {
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    //请求连接地址
    final static String SOJSON_WEATHER_URL = "http://t.weather.sojson.com/api/weather/city/{1}";
    final static String TIAN_QI_API = "https://tianqiapi.com/api?version=v6&appid=84942157&appsecret=BtICC46e";
    final static String API_GET_CODE = "https://api.eteams.cn/oauth2/authorize?corpid=db5bfcc3f340e7b0f6ca4f47b0521f17&response_type=code";
    final static String POST_ACESS_TOKEN = "https://api.eteams.cn/oauth2/access_token";
    final static String GET_USERS = "https://api.eteams.cn/user/v3/findAllUsers?access_token={1}";
    /**
     * 获取数据
     *
     * @param id cityId
     * @return 天气数据
     */
    @Cacheable(cacheNames = "weather_cache", key = "#id")// 从缓存获取，key为ID，缓存具体看 ehcache.xml 配置文件
    public WeatherDto getById(String id) {
        logger.info("WeatherManager#getById: id={}", id);

        try {
            RestTemplate restTemplate = new RestTemplate();
            WeatherDto dto = restTemplate.getForObject(SOJSON_WEATHER_URL, WeatherDto.class, id);

            if (dto != null && dto.isSuccess()) {
                return dto;
            } else {
                logger.error("获取天气数据返回错误：{}", dto);
            }
        } catch (RestClientException e) {
            logger.error("获取天气数据返回错误，出现异常.", e);
        }
        return null;
    }

    /**
     * 获取实况天气数据
     */
    @Cacheable(cacheNames = "liveWeather")
    public LiveWeather getWeather() {
        try {
            RestTemplate restTemplate = new RestTemplate();
            CodeDto codeDto = restTemplate.getForObject(API_GET_CODE, CodeDto.class);
            LiveWeather weather = restTemplate.getForObject(TIAN_QI_API, LiveWeather.class);


            //language=JSON
            //String requestJson = "{\n" +
            //        "  " +
            //        "\"app_key\":\"80eb431d59450ed458b4cb7e34eb2080\",\n" +
            //        "  \"app_secret\": \"93d389bf80720045131ad3299f851f40\",\n" +
            //        "  \"grant_type\": \"authorization_code\",\n" +
            //        "  \"code\": \"" + codeDto.getCode() +
            //        "\"}";
//            MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
//            //Object appKey = "80eb431d59450ed458b4cb7e34eb2080";
//            //Object appSecret = "93d389bf80720045131ad3299f851f40";
//            //Object grantType = "authorization_code";
//            //Object code = codeDto.getCode();
//            param.add("app_key", "80eb431d59450ed458b4cb7e34eb2080");
//            param.add("app_secret", "93d389bf80720045131ad3299f851f40");
//            param.add("grant_type", "authorization_code");
//            param.add("code", codeDto.getCode());

//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
//            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(param, headers);
//            AcessTokenDto acessTokenDto = restTemplate.postForObject(POST_ACESS_TOKEN, entity, AcessTokenDto.class);
//            UsersDto usersDto = restTemplate.getForObject(GET_USERS, UsersDto.class, acessTokenDto.getAccessToken());
            if (weather != null) {
                return weather;
            } else {
                logger.error("获取天气数据返回错误");
            }
        } catch (RestClientException e) {
            logger.error("获取天气数据返回错误，出现异常.", e);
        }
        return null;
    }

    /**
     * 获取员工信息
     */
}
